package com.example.video

import android.os.Bundle
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.ViewCompat
import androidx.core.view.WindowInsetsCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.example.base.mvvm.BaseActivity
import com.example.video.adapter.CarAdapter
import com.example.video.databinding.ActivityCarBinding

@Route(path = "/app/CarActivity")
class CarActivity : BaseActivity<ActivityCarBinding,CarViewModel>() {
    lateinit var carAdapter: CarAdapter

    override fun initView() {
        carAdapter = CarAdapter()
        binding.rvCar.adapter = carAdapter
        binding.rvCar.layoutManager = LinearLayoutManager(this)
        viewModel.car("true","10","1")

        //点击item上的全选+ —
        carAdapter.addChildClickViewIds(R.id.car_cb,R.id.car_add,R.id.car_sub)
        carAdapter.setOnItemChildClickListener { adapter, view, position ->
            when (view.id){
                R.id.car_cb->{
                    carAdapter.data[position].isCheck = !carAdapter.data[position].isCheck
                }
                R.id.car_sub->{
                    if(carAdapter.data[position].cartNum==1){
                        viewModel.delete(arrayOf(carAdapter.data[position].id))
                        carAdapter.data.removeAt(position)
                        carAdapter.notifyItemRemoved(position)
                    }else{
                        carAdapter.data[position].cartNum--
                        //页面
                        (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = carAdapter.data[position].cartNum.toString()

                        viewModel.update(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                    }
                }
                R.id.car_add->{
                    carAdapter.data[position].cartNum++
                    //页面
                    (carAdapter.getViewByPosition(position,R.id.car_num) as TextView).text = carAdapter.data[position].cartNum.toString()

                    viewModel.update(carAdapter.data[position].id,carAdapter.data[position].cartNum)
                }
            }
            checkAll()//计算全选和金额
        }
        //全选的点击事件
        binding.cbAll.setOnCheckedChangeListener { buttonView, isChecked ->
            binding.cbAll.setOnClickListener {
                carAdapter.data.forEach {
                    it.isCheck = isChecked
                }
                carAdapter.notifyDataSetChanged()
                checkAll()
            }
        }
    }

    private fun checkAll() {
        //金额
        var amount = 0f
        carAdapter.data.forEach {
            if(it.isCheck){
                amount+=it.cartNum*it.price.toFloat()
            }
        }
        //循环结束了在赋值
        binding.amountAll.text = "合计$amount 元"
        //计算全选  将所有选中转态保存起来，判断里面是否有false
        val list = mutableListOf<Boolean>()
        carAdapter.data.forEach {
           list.add(it.isCheck)
        }
        if(false in list){
            binding.cbAll.isChecked = false
        }else{
            binding.cbAll.isChecked = true
        }
    }

    override fun initData() {
        viewModel.carSuccess.observe(this){
            if(it.code==200){
                carAdapter.data.clear()
                carAdapter.data.addAll(it.data.list)
                carAdapter.notifyDataSetChanged()
            }
        }
    }

    override fun getViewModel(): Class<CarViewModel> {
        return CarViewModel::class.java
    }

    override fun getLayout(): Int {
        return  R.layout.activity_car
    }

}